﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

using CloudSoft.Extensions;

using ERPStore.Extensions;

namespace ERPStore.Web.Extensions
{
	public static class UriExtensions
	{
		//public static NameValueCollection GetParameters(this Uri uri)
		//{
		//	if (uri == null)
		//	{
		//		return new NameValueCollection();
		//	}
		//	var result = System.Web.HttpUtility.ParseQueryString(uri.Query);
		//	if (result == null)
		//	{
		//		result = new NameValueCollection();
		//	}
		//	return result;
		//}

		public static NameValueCollection ToHtmlDecodedNameValueDictionary(this string query)
		{
			if (query.IsNullOrTrimmedEmpty() || query.IndexOf("?") == -1)
			{
				return new System.Collections.Specialized.NameValueCollection();
			}
			var parameters = query.Split('?')[1].Split('&');
			var result = new NameValueCollection();
			foreach (var item in parameters)
			{
				var tokens = item.Split('=');
				if (tokens.Count() == 2)
				{
					var key = System.Web.HttpUtility.UrlDecode(tokens[0]);
					var value = System.Web.HttpUtility.UrlDecode(tokens[1]);
					result.Add(key, value);
				}
			}
			return result;
		}

		public static string EncodedLink(this ERPStore.Models.Product product)
		{
			if (product == null)
			{
				return null;
			}
			var link = (product.Link.IsNullOrTrimmedEmpty()) ? SEOHelper.SEOUrlEncode(product.Title) : product.Link;
			return link.ToLower();
		}

		[Obsolete("use leadsourceservice instead", false)]
		public static string GetSearchKeywords(this Uri referer)
		{
			if (referer == null)
			{
				return null;
			}

			/* Recherche du domaine */
			var domain = referer.Host;
			NameValueCollection prms = null;
			try
			{
				prms = System.Web.HttpUtility.ParseQueryString(referer.Query.ToLower());
			}
			catch (Exception ex)
			{
				var logger = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Logging.ILogger>();
				logger.Error(ex);
			}

			if (prms == null)
			{
				return null;
			}

			string k = null;
			if (domain.IndexOf("google") > -1)
			{
				k = prms["q"];
			}
			else if (domain.IndexOf("yahoo") > -1)
			{
				k = prms["p"];
			}
			else if (domain.IndexOf("msn") > -1)
			{
				k = prms["q"];
			}
			else if (domain.IndexOf("voila") > -1)
			{
				k = prms["kw"];
			}
			else if (domain.IndexOf("aliceadsl") > -1)
			{
				k = prms["qs"];
			}
			else if (domain.IndexOf("club-internet") > -1)
			{
				k = prms["q"];
			}
			else if (domain.IndexOf("sedoparking") > -1)
			{
				k = prms["keyword"];
			}
			else if (domain.IndexOf("aol") > -1)
			{
				k = prms["query"];
			}
			else if (domain.IndexOf("lycos") > -1)
			{
				k = prms["query"];
			}
			else if (domain.IndexOf("ask") > -1)
			{
				k = prms["q"];
			}
			else if (domain.IndexOf("altavista") > -1)
			{
				k = prms["q"];
			}
			else if (domain.IndexOf("mozbot") > -1)
			{
				k = prms["q"];
			}
			else if (domain.IndexOf("localhost") > -1)
			{
				k = "meuleuse";
				// k = prms["q"];
			}
			else if (domain.IndexOf("leguide") > -1)
			{
				k = System.Web.HttpUtility.UrlDecode(prms["ms"]);
				// k += " | " + prms["slkw"];
			}
			else if (domain.IndexOf("webmarchand.com") > -1)
			{
				if (referer.PathAndQuery.IndexOf("/") != -1)
				{
					var parts = referer.PathAndQuery.Split('/').ToList();
					foreach (var part in parts)
					{
						if (part.Equals("mot"))
						{
							k = parts[parts.IndexOf(part) + 1];
							k = k.Replace("_", " ");
							break;
						}
					}
				}
			}
			else if (domain.IndexOf("shopping.cherchons.com") > -1)
			{
				k = prms["parameter"];
			}
			else if (domain.IndexOf("cherchons.com") > -1)
			{
				k = prms["ms"];
			}
			else if (domain.IndexOf("bing.") > -1)
			{
				k = prms["q"];
			}
			else if (domain.IndexOf("idealo.") > -1)
			{
				k = prms["q"];
			}

			if (k != null)
			{
				return k.Trim();
			}
			return null;

		}


	}
}
